package com.love.marriage.offlineServiceProvider.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.common.exceptions.SystemException;
import com.love.marriage.offlineServiceProvider.entity.OfflineServiceProvider;
import com.love.marriage.offlineServiceProvider.mapper.OfflineServiceProviderMapper;
import com.love.marriage.offlineServiceProvider.service.OfflineServiceProviderService;
import com.love.marriage.offlineServiceProvider.vo.OfflineServiceProviderVo;
import com.love.marriage.partnerCategoryConfig.mapper.PartnerCategoryConfigMapper;
import com.love.marriage.partnerCategoryConfig.vo.PartnerCategoryConfigVo;
import com.love.marriage.userMembershipInfo.service.UserMemberPrivilegeService;
import com.love.marriage.utils.StringUtils;
import com.love.marriage.utils.UserUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * description:  线下服务商表服务实现类
 * author:       alex
 * createDate:   2024-12-08 12:08:27
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
public class OfflineServiceProviderServiceImp extends ServiceImpl<OfflineServiceProviderMapper, OfflineServiceProvider> implements OfflineServiceProviderService {

    private final OfflineServiceProviderMapper offlineServiceProviderMapper;

    private final PartnerCategoryConfigMapper partnerCategoryConfigServiceMapper;

    private final UserMemberPrivilegeService userMemberPrivilegeService;

    @Override
    public Page<OfflineServiceProviderVo> getPage(Long pageNum, Long pageSize, OfflineServiceProviderVo offlineServiceProviderVo) {
        Page<OfflineServiceProviderVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        return offlineServiceProviderMapper.getPage(page, offlineServiceProviderVo);
    }

    @Override
    public OfflineServiceProviderVo queryOfflineServiceProvider(Long id) {
        return offlineServiceProviderMapper.queryOfflineServiceProvider(id);
    }

    @Override
    public OfflineServiceProviderVo queryOfflineServiceProviderByUserId(Long userId, String type) {
        return offlineServiceProviderMapper.queryOfflineServiceProviderByUserId(userId, type);
    }

    @Override
    public OfflineServiceProviderVo addOfflineServiceProvider(OfflineServiceProviderVo offlineServiceProviderVo) {
        if (offlineServiceProviderVo.getUserId() == null && UserUtils.getLoginUser() != null) {
            offlineServiceProviderVo.setUserId(UserUtils.getLoginUser().getId());
        }
        OfflineServiceProvider offlineServiceProvider = new OfflineServiceProvider();
        BeanUtils.copyProperties(offlineServiceProviderVo, offlineServiceProvider);
        offlineServiceProviderMapper.insert(offlineServiceProvider);
        OfflineServiceProviderVo res = new OfflineServiceProviderVo();
        BeanUtils.copyProperties(offlineServiceProvider, res);
        return res;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OfflineServiceProviderVo addInfo(OfflineServiceProviderVo offlineServiceProviderVo) {
        OfflineServiceProviderVo db = offlineServiceProviderMapper.queryOfflineServiceProviderByUserId(offlineServiceProviderVo.getUserId(), offlineServiceProviderVo.getType());
        if ("partner".equals(offlineServiceProviderVo.getType()) &&
                (db == null || StringUtils.isBlank(db.getPartnerType()) || db.getStatus() == 1 ||
                        !db.getPartnerType().equals(offlineServiceProviderVo.getPartnerType()))) {
            PartnerCategoryConfigVo query = new PartnerCategoryConfigVo();
            query.setCategoryCode(offlineServiceProviderVo.getPartnerType());
            PartnerCategoryConfigVo partnerCategoryConfigVo = partnerCategoryConfigServiceMapper.queryPartnerCategoryConfigInfo(query);
            if (partnerCategoryConfigVo == null) {
                throw new SystemException("1800001", "请输入正确的合作伙伴类型");
            }
            ChronoUnit cur;
            switch (partnerCategoryConfigVo.getDurationType()) {
                case "DAYS":
                    cur = ChronoUnit.DAYS;
                    break;
                case "MONTHS":
                    cur = ChronoUnit.MONTHS;
                    break;
                case "YEARS":
                    cur = ChronoUnit.YEARS;
                    break;
                default:
                    cur = ChronoUnit.DAYS;
            }
            if (offlineServiceProviderVo.getStatus() == 2) {
                offlineServiceProviderVo.setStartTime(LocalDateTime.now());
                offlineServiceProviderVo.setEndTime(LocalDateTime.now().plus(partnerCategoryConfigVo.getDuration(),
                        cur));
            }
        }
        OfflineServiceProviderVo res;
        if (db == null) {
            res = addOfflineServiceProvider(offlineServiceProviderVo);
        } else {
            offlineServiceProviderVo.setId(db.getId());
            res = updateOfflineServiceProvider(offlineServiceProviderVo);
        }
        // 添加会员、红娘、合伙人相关权益
        userMemberPrivilegeService.addMemberPrivilege(offlineServiceProviderVo.getUserId());
        return res;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OfflineServiceProviderVo addOrUpdateInfo(OfflineServiceProviderVo offlineServiceProviderVo) {
        OfflineServiceProviderVo db = offlineServiceProviderMapper.queryOfflineServiceProviderByUserId(offlineServiceProviderVo.getUserId(), offlineServiceProviderVo.getType());
        OfflineServiceProviderVo res;
        if (db == null) {
            res = addOfflineServiceProvider(offlineServiceProviderVo);
        } else {
            offlineServiceProviderVo.setId(db.getId());
            res = updateOfflineServiceProvider(offlineServiceProviderVo);
        }
        return res;
    }

    @Override
    public OfflineServiceProviderVo updateOfflineServiceProvider(OfflineServiceProviderVo offlineServiceProviderVo) {
        OfflineServiceProvider offlineServiceProvider = new OfflineServiceProvider();
        BeanUtils.copyProperties(offlineServiceProviderVo, offlineServiceProvider);
        offlineServiceProviderMapper.updateById(offlineServiceProvider);
        return offlineServiceProviderVo;
    }

    @Override
    public Boolean deleteOfflineServiceProvider(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return true;
        }
        List<String> idArr = Arrays.asList(ids.split(","));
        offlineServiceProviderMapper.deleteBatchIds(idArr);
        return true;
    }

    // 审批线下服务商
    @Override
    public Boolean approveOfflineServiceProvider(Long id, Integer status) {
        OfflineServiceProviderVo offlineServiceProviderVo = queryOfflineServiceProvider(id);
        if (offlineServiceProviderVo == null) {
            throw new SystemException("1800006", "数据不存在！");
        }
        offlineServiceProviderVo.setStatus(status);
        updateOfflineServiceProvider(offlineServiceProviderVo);
        return true;
    }
}
